<template>
   <div class="login_container">
      <div class="login_box">
          <!-- 头像区域 -->
          <div class="login_avatar">
              <img src="../assets/login_avatar.png" alt="" />
          </div>

          <!-- 登录表单区域 -->
          <div class="login_form">
              <el-form :model="loginForm" :rules="loginFormRules" ref="loginFormRef" label-width="0px">
                  <!-- 用户名 -->
                <el-form-item prop="account">
                    <el-input v-model="loginForm.account" prefix-icon="el-icon-user"></el-input>
                </el-form-item>

                <!-- 密码 -->
                <el-form-item prop="password">
                    <el-input v-model="loginForm.password" prefix-icon="el-icon-lock" type="password"></el-input>
                </el-form-item>

                <!-- 按钮 -->
                <el-form-item class="btns">
                    <el-button type="primary" @click="login()">登录</el-button>
                    <el-button type="info" @click="ResetLoginForm()">重置</el-button>
                </el-form-item>
              </el-form>
          </div>
      </div>
   </div>
</template>

<script>
export default {
    data() {
        return {
            loginForm:{
                account: "",
                password: ""
            },
            loginFormRules:{
                account:[
                    {required: true, message: "用户名不能为空", trigger: "blur"}
                ],
                password:[
                    {required: true, message: '密码不能为空', trigger: 'blur'}
                ]
            }
        }
    },
    methods: {
        // 重置表单
        ResetLoginForm () {
            this.$refs.loginFormRef.resetFields();
        },
        // 登录
        login (){
            this.$refs.loginFormRef.validate(valid => {
                if(!valid) return;
                this.$axios.post("login/check",this.loginForm).then((result) => {
                    if(result.data.code !== "200") return this.$message.error("登录失败!");
                    this.$message.success("登录成功");
                    window.sessionStorage.setItem("token",result.data.data.token);
                    this.$router.push("/index")
                });
               
            });
        }
    }
    
}
</script>

<style lang="less" scoped>
.login_container {
    background-color: #2b4b6b;
    height: 100%;
}

.login_box {
    width: 450px;
    height: 300px;
    background-color: #fff;
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
}

.login_avatar {
    width: 130px;
    height: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    box-shadow: 0 0 10px #ddd;
    margin: 0 auto;
    transform: translateY(-50%);

    img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background-color: #eee;
    }
}

.btns {
    float: right;
}
.login_form {
    position: absolute;
    width: 100%;
    padding: 0 20px;
    bottom: 0px;
    box-sizing: border-box;
}

</style>

